package com.urbanski.util.primitives;

/**
 * This class represents a boolean that can be modified without creating a new instance.
 * Its functionality is equivalent to the Boolean class in all but that regard.  All Boolean
 * methods, other than the static methods that do not return Boolean, have been replicated.
 * Under the hood, some methods are performed by reusing Boolean methods.
 * 
 * @author murbanski004
 *
 */
public class MutableBoolean implements Comparable<MutableBoolean>
{
	private boolean bool;
	
	/**
	 * Constructs a new MutableBoolean with the specified value
	 * @param value the value of this MutableBoolean
	 */
	public MutableBoolean(boolean value)
	{
		bool = value;
	}
	/**
	 * Constructs a new MutableBoolean with a value obtained
	 * by parsing the specified String as per Boolean.parseBoolean.
	 * @param s the String to parse to the value of this MutableBoolean
	 */
	public MutableBoolean(String s)
	{
		bool = Boolean.parseBoolean(s);
	}
	public boolean booleanValue()
	{
		return bool;
	}
	public int compareTo(MutableBoolean b)
	{
		return bool != b.bool ? (bool ? 1 : -1) : 0;
	}
	@Override
	public int hashCode()
	{
		return Boolean.valueOf(bool).hashCode();
	}
	@Override
	public boolean equals(Object obj)
	{
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!(obj instanceof MutableBoolean))
			return false;
		MutableBoolean other = (MutableBoolean) obj;
		return bool == other.bool;
	}
	public static MutableBoolean valueOf(boolean b)
	{
		return new MutableBoolean(b);
	}
	public static MutableBoolean valueOf(String s)
	{
		return new MutableBoolean(s);
	}
	/**
	 * Sets the value of this MutableBoolean
	 * 
	 * @param i the value to set
	 */
	public void setValue(boolean b)
	{
		bool = b;
	}
	/**
	 * Negates the value of this MutableBoolean and returns itself.  This
	 * can be used to chain expressions.
	 * @return a reference to this MutableBoolean after negating its value.
	 */
	public MutableBoolean negate()
	{
		bool = !bool;
		return this;
	}
}
